package com.example.shop.controller;

import com.example.shop.bean.CategoryBean;
import com.example.shop.mapper.CategoryMapper;
import com.example.shop.util.NotNullUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Controller
@RequestMapping("/category")
public class CategoryController extends BaseController{
    @Autowired
    CategoryMapper categoryMapper;


    @RequestMapping("/list")
    public String list(HttpServletRequest req){  //selectList是 baseMapper内部的封装方法，sql
        req.setAttribute("retList",categoryMapper.selectList(null));
        return "/category/list";
    }
    @RequestMapping("/del")
    public String del(int id){
        categoryMapper.deleteById(id);
        return "redirect:/category/list";
    }
    @GetMapping("/add")   //<a href="
    public String add(Integer id,HttpServletRequest req){
        req.setAttribute("cateList",categoryMapper.selectList(null));
        req.setAttribute("bean",id==null?null:categoryMapper.selectById(id));
        return "/category/add";
    }
    @PostMapping("/add")  //<form method ="post"
    public String add(CategoryBean bean, HttpServletResponse resp){
        if(NotNullUtil.isBlank(bean)){
            return jsAlert("请完善信息!",resp);
        }
        try{
            if(bean.id==null){
                //bean.ctime=new Date();
                categoryMapper.insert(bean);
            }else{
                categoryMapper.updateById(bean);
            }
        }catch (Exception a){
            return jsAlert("类别名称不能重复!",resp);
        }

        return "redirect:/category/list";
    }
}
